<?php

App::uses('AppController', 'Controller');

/**
 * Vehicles Controller
 *
 * @property Vehicle $Vehicle
 */
class VehiclesController extends AppController {

  function beforeFilter() {
    $this->Auth->allow('selectChange');
    $this->set('title_for_layout', 'Camiones');
  }

  public function search() {
    $name = '%' . trim($this->request->data('name')) . '%';
    $this->paginate = array(
        'conditions' => array('OR' => array(
                'Company.social_razon LIKE' => $name,
                'Vehicle.driver LIKE' => $name
        )));
    $this->set('vehicles', $this->paginate());
    $this->layout = 'ajax';
  }

  public function selectChange() {
    $this->set('vehicles', $this->getVehiclesList($this->request->data('idCompany')));
    $this->layout = 'ajax';
  }

  public function getVehiclesList($idCompany = null) {

    if (isset($idCompany)) {
      $conditions = array('Vehicle.company_id' => $idCompany);
    } else {
      $conditions = array();
    }
    $vehicles = $this->Vehicle->find('all', array(
        'order' => array(
            'Vehicle.model ASC',
            'Vehicle.domain ASC'
        ),
        'fields' => array(
            'Vehicle.id',
            'Vehicle.model',
            'Vehicle.domain'
        ),
        'conditions' => $conditions,
        'recursive' => 0
            ));

    $vehiclesList = array();
    foreach ($vehicles as $vehicle) {
      $vehiclesList[$vehicle['Vehicle']['id']] = $vehicle['Vehicle']['model'] . ' - ' . $vehicle['Vehicle']['domain'];
    }
    return $vehiclesList;
  }

  public function selectOne() {

    $this->loadModel('Driver');
    $drivers = $this->Driver->find('first', array('fields' => array('Driver.vehicle_id'), 'conditions' => array('Driver.id' => $this->request->data('idDriver'))));
    $vehicleId = $drivers['Driver']['vehicle_id'];
    $this->set('vehicles', $this->getVehiclesListOne($vehicleId));
    $this->layout = 'ajax';
  }

  public function getVehiclesListOne($idVehicle = null) {

    if (isset($idVehicle)) {
      $conditions = array('Vehicle.id' => $idVehicle);
    } else {
      $conditions = array();
    }


    $vehicles = $this->Vehicle->find('all', array(
        'order' => array(
            'Vehicle.model ASC',
            'Vehicle.domain ASC'
        ),
        'fields' => array(
            'Vehicle.id',
            'Vehicle.model',
            'Vehicle.domain'
        ),
        'conditions' => $conditions,
        'recursive' => 0
            ));

    $vehiclesList = array();
    foreach ($vehicles as $vehicle) {
      $vehiclesList[$vehicle['Vehicle']['id']] = $vehicle['Vehicle']['model'] . ' - ' . $vehicle['Vehicle']['domain'];
    }
    return $vehiclesList;
  }

  /**
   * index method
   *
   * @return void
   */
  public function index() {
    $this->Vehicle->recursive = 0;
    $this->paginate = array('order' => 'Vehicle.id desc', 'limit' => 50);
    $this->set('vehicles', $this->paginate());
  }

  public function indexAjax() {
    $this->Vehicle->recursive = 0;
    $this->paginate = array('order' => 'Vehicle.id desc', 'limit' => 50);
    $this->set('vehicles', $this->paginate());
    $this->layout = 'ajax';
  }

  /**
   * view method
   *
   * @throws NotFoundException
   * @param string $id
   * @return void
   */
  public function view($id = null) {
    if (!$this->Vehicle->exists($id)) {
      throw new NotFoundException(__('Invalid vehicle'));
    }
    $options = array('conditions' => array('Vehicle.' . $this->Vehicle->primaryKey => $id));
    $this->set('vehicle', $this->Vehicle->find('first', $options));
  }

  /**
   * add method
   *
   * @return void
   */
  public function add() {
    if ($this->request->is('post')) {
      $this->Vehicle->create();
      if ($this->Vehicle->save($this->request->data)) {
        $this->Session->setFlash(__('El Camion ha sido creado exitosamente'));
        $this->redirect(array('action' => 'index'));
      } else {
        $this->Session->setFlash(__('El Camion no se guardado, Intente Nuevamente.'));
      }
    }
    $companies = $this->Vehicle->Company->find('list');
    $this->set(compact('companies'));
  }

  public function addAll($model = null, $domain = null, $company_id = null) {

    // Creo los datos en un array        
    $data = array(
        'Vehicle' => array('model' => $model, 'domain' => $domain, 'company_id' => $company_id)
    );
    $this->Vehicle->create();
    if ($this->Vehicle->save($data)) {
      return $this->Vehicle->getId();
    }
  }

  public function addAjax() {
    if ($this->request->is('post')) {
      $this->Vehicle->create();
      if ($this->Vehicle->save($this->request->data)) {
        $this->Session->setFlash(__('El Camion ha sido creado exitosamente'));
        $this->redirect('/');
      } else {
        $this->Session->setFlash(__('El Camion no se guardado, Intente Nuevamente.'));
      }
    }
    $companies = $this->Vehicle->Company->find('list');
    $this->set(compact('companies'));
    $this->layout = 'ajax';
  }

  /**
   * edit method
   *
   * @throws NotFoundException
   * @param string $id
   * @return void
   */
  public function edit($id = null) {


    $this->Vehicle->id = $id;


    if (!$this->Vehicle->exists($id)) {
      throw new NotFoundException(__('Invalid vehicle'));
    }
    if ($this->request->is('post') || $this->request->is('put')) {
      if ($this->Vehicle->save($this->request->data)) {
        $this->Session->setFlash(__('El Camion ha sido actualizado exitosamente'));
        $this->redirect(array('action' => 'index'));
      } else {
        $this->Session->setFlash(__('El Camion NO se actualizado, Intente Nuevamente.'));
      }
    } else {
      $options = array('conditions' => array('Vehicle.' . $this->Vehicle->primaryKey => $id));
      $this->request->data = $this->Vehicle->find('first', $options);
    }
    $companies = $this->Vehicle->Company->find('list');
    $this->set(compact('companies'));
  }

  /**
   * delete method
   *
   * @throws NotFoundException
   * @throws MethodNotAllowedException
   * @param string $id
   * @return void
   */
  public function delete($id = null) {
    $this->Vehicle->id = $id;
    if (!$this->Vehicle->exists()) {
      throw new NotFoundException(__('Invalid vehicle'));
    }
    $this->request->onlyAllow('post', 'delete');
    if ($this->Vehicle->delete()) {
      $this->Session->setFlash(__('Camion Borrado exitosamente'));
      $this->redirect(array('action' => 'index'));
    }
    $this->Session->setFlash(__('Camion No Borrado'));
    $this->redirect(array('action' => 'index'));
  }

}
